package com.xtm.exercise.factory;

/***
 *@Author: 王磊
 *@CreateTime: 2025-06-13  09:05
 *@Description: TODO
 *@title: ActionFactory
 */

import com.xtm.exercise.service.Action;
import com.xtm.exercise.service.impl.executeActionOne;
import com.xtm.exercise.service.impl.executeActionThree;
import com.xtm.exercise.service.impl.executeActionTwo;

import java.util.HashMap;
import java.util.Map;

/**
 * 工厂类
 */
public class ActionFactory {

    //    私有构造方法
    private ActionFactory() {

    }

    //    单例
    private static class SingleHandler {
        private static ActionFactory instance = new ActionFactory();
    }

    //    获得工厂类实例
    public static ActionFactory getInstance() {
        return SingleHandler.instance;
    }

    private static final Map<String, Action> ACTION_MAP = new HashMap<>();

    //    工厂类实例放在 map 中
    static {

        ACTION_MAP.put("1101", new executeActionOne());
        ACTION_MAP.put("1102", new executeActionTwo());
        ACTION_MAP.put("1104", new executeActionThree());

    }

    //    根据不同的编码获得不同的实现类实例
    public static Action getAction(String actionCode) {

        Action action = ACTION_MAP.get(actionCode);

        if (action == null) {
            throw new RuntimeException("无效 actionCode");
        }
        return action;

    }

    //    调用实现类所在方法
    public void doAction(String actionCode) {
        getAction(actionCode).doSomeAction(actionCode);
    }
}